package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class TextViewsActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_hello;
    private TextView tv_hello_bg;
    private TextView tv_hello_size;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textviews);
        tv_hello = findViewById(R.id.tv_hello);
        tv_hello_bg = findViewById(R.id.tv_hello_bg);
        tv_hello_size = findViewById(R.id.tv_hello_size);

        tv_hello.setOnClickListener(this);
        tv_hello_bg.setOnClickListener(this);
        tv_hello_size.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_hello) {
            tv_hello.setText("点击后显示的字小了，颜色变了");
            // 默认单位sp((TypedValue.COMPLEX_UNIT_SP)
            tv_hello.setTextSize(10F);
            //textView.setTextColor(Color.BLUE);
            //0x透明度（两位）Red(两位)Green(两位)Blue(两位)
            tv_hello.setTextColor(0xEE0000EE);
        } else if (v.getId() == R.id.tv_hello_bg) {
            float curr = tv_hello_bg.getTextSize();
            float next = curr * 1.02f;
            tv_hello_bg.setText("点击后显示带背景色："+curr+"->"+next);
            // 默认设置单位sp((TypedValue.COMPLEX_UNIT_SP)，默认取值单位px，这点要注意~
            tv_hello_bg.setTextSize(TypedValue.COMPLEX_UNIT_PX,next);
            Toast.makeText(this,"字号:"+curr+"->"+next,Toast.LENGTH_LONG).show();
            //textView.setTextColor(Color.BLUE);
            //0x透明度（两位）Red(两位)Green(两位)Blue(两位)
            tv_hello_bg.setTextColor(0xFF0003EE);
            // 六位颜色 Xml中默认不透明，代码中默认为透明，如下代码设置即为透明
//            tv_hello_bg.setTextColor(0x0003EE);
            tv_hello_bg.setBackgroundColor(Color.GRAY);
//            tv_hello_bg.setBackgroundResource(R.color.black);
        } else if (v.getId() == R.id.tv_hello_size) {
            // 设置大小同样涉及到尺寸单位问题需要注意
            ViewGroup.LayoutParams layoutParams = tv_hello_size.getLayoutParams();
            Log.i("TextView", "设置前"+layoutParams.height + "==" + layoutParams.width);
            layoutParams.width = layoutParams.width <100 || layoutParams.width >500?100 : layoutParams.width +20;
            layoutParams.height = layoutParams.height <100 || layoutParams.height >300?100 : layoutParams.height +20;
            tv_hello_size.setLayoutParams(layoutParams);
            Log.i("TextView", "设置后"+layoutParams.height + "==" + layoutParams.width);
            tv_hello_size.setText("持续点击尺寸会一直变化");
        }
    }
}